package com.base.cn.platform.os.service.order.check;

import com.base.cn.platform.os.common.utils.ObjectUtils;
import com.base.cn.platform.os.common.utils.order.ShopType;
import com.base.cn.platform.os.common.utils.result.ResultUtil;
import com.base.cn.platform.os.entity.op.activity.OpActivity;
import com.base.cn.platform.os.entity.order.shop.ShopData;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 活动验证对象
 *
 * @author s.li
 * @date 2019/05/27
 */
public class ActivityCheckObserver extends OrderCheckObserver {

    public ActivityCheckObserver(CreateOrderDataFrom createOrderDataFrom) {
        this.createOrderDataFrom = createOrderDataFrom;
        this.createOrderDataFrom.addObserver(this);
    }

    @Override
    public Map<String, Object> orderCheck() {
        OpActivity activity =createOrderDataFrom.getActivity();
        if(activity==null && !createOrderDataFrom.isQueryActivity()){
            activity = opActivityBiz.findById(createOrderDataFrom.getActivityId());
            createOrderDataFrom.setQueryActivity(true);
            createOrderDataFrom.setActivity(activity);
        }
        Map<String,Object> errorResultMap = ResultUtil.ERROR();
        errorResultMap.put("errorType","ACTIVITY");
        if(!ObjectUtils.isNotEmpty(activity)){
            ResultUtil.setMessage(errorResultMap,"活动不存在");
            return errorResultMap;
        }
        int releaseStatus =activity.getReleaseStatus();
        if(releaseStatus!=1){
            ResultUtil.setMessage(errorResultMap,"活动未发布");
            return errorResultMap;
        }
        int status =activity.getStatus();
        if(status !=1){
            ResultUtil.setMessage(errorResultMap,"活动不可用");
            return errorResultMap;
        }
        Date currentDate = new Date();
        Date beginTime = activity.getBeginTime();
        Date endTime = activity.getEndTime();
        if(beginTime.after(currentDate)){
            ResultUtil.setMessage(errorResultMap,"活动未开始");
            return errorResultMap;
        }
        if(currentDate.after(endTime)){
            ResultUtil.setMessage(errorResultMap,"活动已结束");
            return errorResultMap;
        }
        //以商品类型进行分组
        Map<String, List<ShopData>> shopDataListMap = createOrderDataFrom.getShopList().stream().collect(Collectors.groupingBy(e->e.getShopType()));
        List<ShopData> accountShopDataList = shopDataListMap.get(ShopType.ORDER_SHOP_ACCOUNT);
        if(ObjectUtils.isNotEmpty(accountShopDataList)){
            ResultUtil.setMessage(errorResultMap,"充值订单不可使用活动优惠");
            return errorResultMap;
        }
        //活动最低消费限制
        double askAmount = activity.getAskAmount();

        //获取商品的价格
        double shopPrice = createOrderDataFrom.getShopList().stream().mapToDouble(e->e.getRealPrice()).sum();
        //获取商品的会员价格
        double shopMemberPrice = createOrderDataFrom.getShopList().stream().mapToDouble(e->e.getMemberPrice()).sum();

        if(createOrderDataFrom.isMemberUser()){//会员用户
            if(askAmount > shopMemberPrice){
                ResultUtil.setMessage(errorResultMap,"订单金额未达到活动最低限额【￥"+askAmount+"】");
                return errorResultMap;
            }
        }else{//非会员用户
            if(askAmount > shopPrice){
                ResultUtil.setMessage(errorResultMap,"订单金额未达到活动最低限额【￥"+askAmount+"】");
                return errorResultMap;
            }
        }
        if(activity.getActivityType().contains("1")){//是满减的，计算价格
            //减免的金额
            double subAmount = activity.getSubAmount();
            //重新设置价格
            this.setSubPrice(createOrderDataFrom.getShopList(),subAmount);
        }
        return ResultUtil.SUCCESS();
    }

    /**
     * 设置减免金额
     * @param shopList 商品列表
     * @param subPrice 减免的金额
     */
    private void setSubPrice(List<ShopData> shopList, double subPrice){
        //商品数据存在才可能使用优惠券
        if(ObjectUtils.isNotEmpty(shopList)){
            //商品总价格
            double shopSumPrice = shopList.stream().mapToDouble(ShopData::getRealPrice).sum();
            //会员总价格
            double shopMemberSumPrice = shopList.stream().mapToDouble(ShopData::getMemberPrice).sum();
            shopList.forEach(shop ->{
                double _subPrice;
                if(createOrderDataFrom.isMemberUser()
                        && "3".equals(createOrderDataFrom.getSelaSwitch())){//会员价格
                    _subPrice = shop.getMemberPrice() / shopMemberSumPrice * subPrice;
                    double memberPrice = shop.getMemberPrice() - _subPrice;
                    shop.setMemberPrice(memberPrice);
                    shop.setRealPrice(memberPrice);
                }else{//非会员价格
                    _subPrice = shop.getRealPrice() / shopSumPrice * subPrice;
                    shop.setRealPrice(shop.getRealPrice() - _subPrice);
                }
                //优惠券减免金额
                shop.setActivitySubPrice(_subPrice);
            });
        }
    }
}
